<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="授权方式" prop="authType">
        <el-select v-model="queryParams.authType" clearable class="!w-120px">
          <el-option
            v-for="dict in authTypeDictDatas"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="keyWord">
        <el-input placeholder="请输入内容" v-model.trim="keyWord">
          <template #prepend>
            <el-select v-model="kType" placeholder="请选择" class="!w-120px">
              <el-option label="管理人员" value="authUser" />
              <el-option label="地址" value="addrName" />
            </el-select>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button
          type="danger"
          plain
          @click="openForm('community')"
          v-hasPermi="['zwsj:auth-addr:create']"
          >按社区授权</el-button
        >
        <el-button
          type="primary"
          plain
          @click="openForm('house')"
          v-hasPermi="['zwsj:auth-addr:create']"
          >按房屋授权</el-button
        >
        <el-button
          type="warning"
          plain
          @click="openForm('room')"
          v-hasPermi="['zwsj:auth-addr:create']"
          >按房间授权</el-button
        >
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" width="60" />
      <el-table-column label="授权方式" align="center" prop="authType" width="90">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ADDR_AUTH_TYPE" :value="scope.row.authType" />
        </template>
      </el-table-column>
      <el-table-column label="授权地址" align="left" prop="addrName" />
      <el-table-column label="用户类型" align="center" prop="userType" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.USER_TYPE" :value="scope.row.userType" />
        </template>
      </el-table-column>
      <el-table-column label="数据类型" align="center" prop="parentAuthUserId" width="100">
        <template #default="scope">
          <span v-if="scope.row.parentAuthUserId === 0" style="color:green;">主理人</span>
          <span v-else style="color:gray;">代理人</span>
        </template>
      </el-table-column>
      <el-table-column label="管理人员" align="left" prop="authUserName" width="260">
        <template #default="scope">
          <span>{{ scope.row.parentAuthUserName+">>"+scope.row.authUserName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="90"
      >
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zwsj:auth-addr:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 添加或修改对话框 -->
  <AuthAddrForm ref="formRef" @comfirm-auth-addr="getList" />
</template>

<script lang="ts" setup>
import { DICT_TYPE, getStrDictOptions } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import * as AuthAddrApi from '@/api/zwsj/authAddr'
import AuthAddrForm from './AuthAddrForm.vue'

defineOptions({ name: 'AuthAddr' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const authTypeDictDatas = getStrDictOptions(DICT_TYPE.ADDR_AUTH_TYPE)
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据

const keyWord = ref('')
const kType = ref('authUser')
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  authType: undefined,
  addrName: undefined,
  authUserName: undefined
})

const queryFormRef = ref() // 搜索的表单

/** 查询部门列表 */
const getList = async () => {
  loading.value = true
  try {
    queryParams.authUserName = undefined
    queryParams.addrName = undefined
    if ('authUser' == kType.value) {
      queryParams.authUserName = keyWord.value
    } else {
      queryParams.addrName = keyWord.value
    }
    const data = await AuthAddrApi.getAuthAddrPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  keyWord.value = ''
  queryParams.pageNo = 1
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (authType: string) => {
  formRef.value.open(authType)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AuthAddrApi.deleteAuthAddr(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
